package component.appointment;

import java.sql.*;
import java.util.Scanner;
import util.ConnectionUtils;

public class AppointmentRecord {
    public static void viewAppointments(String patientId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = ConnectionUtils.getConn();
            String sql = "SELECT a.id, a.appointment_time, a.status, " +
                        "u.name as doctor_name, d.department, d.doctorRank " +
                        "FROM appointment a " +
                        "JOIN doctor d ON a.doctor_id = d.id " +
                        "JOIN user u ON d.user_id = u.id " +
                        "WHERE a.patient_id = ? " +
                        "ORDER BY a.appointment_time DESC";
            
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, patientId);
            rs = pstmt.executeQuery();

            System.out.println("\n=== 您的预约记录 ===");
            System.out.printf("%-15s %-20s %-10s %-8s %-8s %-10s\n", 
                            "预约号", "预约时间", "医生姓名", "科室", "职称", "状态");
            System.out.println("------------------------------------------------------------------------");

            while (rs.next()) {
                System.out.printf("%-15s %-20s %-10s %-8s %-8s %-10s\n",
                    rs.getString("id"),
                    rs.getTimestamp("appointment_time"),
                    rs.getString("doctor_name"),
                    rs.getString("department"),
                    rs.getString("doctorRank"),
                    getStatusText(rs.getInt("status"))
                );
            }

        } catch (SQLException e) {
            System.err.println("查询预约记录时发生错误: " + e.getMessage());
        } finally {
            ConnectionUtils.close(conn, pstmt, rs);
        }
    }

    private static String getStatusText(int status) {
        switch (status) {
            case 0: return "待就诊";
            case 1: return "已完成";
            case 2: return "已取消";
            default: return "未知状态";
        }
    }

    // 取消预约功能
    public static void cancelAppointment(String patientId, String appointmentId) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = ConnectionUtils.getConn();
            
            // 首先验证该预约是否属于该病人
            String checkSql = "SELECT status FROM appointment WHERE id = ? AND patient_id = ?";
            pstmt = conn.prepareStatement(checkSql);
            pstmt.setString(1, appointmentId);
            pstmt.setString(2, patientId);
            ResultSet rs = pstmt.executeQuery();

            if (!rs.next()) {
                System.out.println("未找到该预约记录！");
                return;
            }

            int status = rs.getInt("status");
            if (status != 0) {
                System.out.println("只能取消待就诊的预约！");
                return;
            }

            // 更新预约状态为已取消
            String updateSql = "UPDATE appointment SET status = 2 WHERE id = ?";
            pstmt = conn.prepareStatement(updateSql);
            pstmt.setString(1, appointmentId);
            
            int result = pstmt.executeUpdate();
            if (result > 0) {
                System.out.println("预约已成功取消！");
            } else {
                System.out.println("取消预约失败，请稍后重试！");
            }

        } catch (SQLException e) {
            System.err.println("取消预约时发生错误: " + e.getMessage());
        } finally {
            ConnectionUtils.close(conn, pstmt, null);
        }
    }
} 